###############################################
#                                             #
#   Experiment #2 - Election Result Reporting #
#     June 2023 sample                        #
#     Created by Mac Lockhart June 29 2023    #
#     Updated June 29 2023                    #
#                                             #
###############################################

library(tidyverse)
library(RColorBrewer)
library(car)
library(data.table)
library(modelsummary)


DF <- fread("National Recoded.csv")
CO <- fread("Colorado Recoded.csv")
GA <- fread("Georgia Recoded.csv")
LA <- fread("LA Recoded.csv")
TX <- fread("Texas Recoded.csv")

DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)

#Recode outcomes as change from pre-treatment values following the pre-analysis plan
DF<-DF %>% mutate(trust_az_post = coalesce(trust_az_post_1, trust_az_post_2)) 
DF$trust_az <- DF$trust_az_post - DF$trust_az_pre

m1.1 <- lm(trust_az ~ az_treatment * az_prebunk, DF)
summary(m1.1)

modelsummary(m1.1, output = "TableS2.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('az_treatmentDelay' = 'Results delayed',
                          "az_prebunkPrebunk"="Viewed pre-bunking video",
                          "az_treatmentDelay:az_prebunkPrebunk" = "Interaction"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1)

DF$trust_az_d <- dplyr::recode(DF$trust_az_post, "4" = "1",
                               '3' = "1",
                               '2' = "0",
                               '1' = "0")
m1.1 <- lm(trust_az_d ~ az_treatment * az_prebunk, DF)
summary(m1.1)

modelsummary(m1.1, output = "TableS1.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('az_treatmentDelay' = 'Results delayed',
                          "az_prebunkPrebunk"="Viewed pre-bunking video",
                          "az_treatmentDelay:az_prebunkPrebunk" = "Interaction"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1)
